/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-25 14:40:27
 * @LastEditors: turingchang
 * @LastEditTime: 2020-08-25 22:52:55
 */

const FileTransport = require('egg-logger').FileTransport;
const moment = require('../utils/time').moment;
 
class CoustomTransport extends FileTransport {    
    constructor(options, ctx) {
        super(options);
        this.ctx = ctx;
    }
 
    log(level, args, meta) {
        //console.log(args);
        const prefixStr = this.buildFormat(level,args);
        // for (let i in args) {
        //     if (args.hasOwnProperty(i)) {
        //         if (parseInt(i, 10) === 0) {
        //             args[i] = `${prefixStr}${args[i]}`;
        //         }
        //         if (parseInt(i, 10) === args.length - 1) {
        //             args[i] += '\n';
        //         }
        //     }
        // } 
        args= [];
        args[0] =prefixStr;
        super.log(level, args, meta);
    }
 
    buildFormat(level,args) {
        var obj ={};
        var loginfo ={};
        var err ={};
        var errmsg ='';
        if(!this.ctx.app.utils.validator.isNull(args)){
            if(args.length>0){
                loginfo =args[0];
                if(args.length>1){
                    if (args[1] instanceof Error) {
                        err = args[1];
                        errmsg =err.message;
                    }
                    err =args[1];                    
                }
            }
        }
        const time =moment().format('YYYY-MM-DD HH:mm:ss.SSS');
        const pid =`${process.pid}`;
        const path =this.ctx.path;
        const method =this.ctx.method;
        const ip = this.ctx.ip;
        const token =this.ctx.request.header.token;
        const cookie =this.ctx.request.header.cookie;        
        var params =this.ctx.query;
        if(method !='get'){
            params =this.ctx.request.body;
        }
        const data =this.ctx.body;   
        
        obj ={
            time:time,
            level:level,
            pid:pid,
            ip:ip,
            path:path,
            token:token,
            //cookie:cookie,
            params:params,
            data:data,
            log:loginfo
        }
        
        var res ='';
        //res +=`${JSON.stringify(time)}：`;
        for(var key in obj){
            if(!this.ctx.app.utils.validator.isNull(key)){  
                if(this.ctx.app.utils.validator.isNull(obj[key])){     
                    switch(typeof obj[key]){
                        case Number:obj[key] =0;break;
                        case String:obj[key] ='';break;
                        case Object:case Array:obj[key] ={};break;
                        default:obj[key] =null;break;
                    }
                    //obj[key] =null;
                }                  
                res +=`${key}:${JSON.stringify(obj[key])},`;
            }            
        }
        res = res.substring(0, res.lastIndexOf(','));
        return res;

        const timeStr = `[${moment().format('YYYY-MM-DD HH:mm:ss.SSS')}]`;
        const threadNameStr = `[${process.pid}]`;
        const urlStr = `[${this.ctx.request.url}]`
        return `${timeStr}${threadNameStr}${urlStr}`;
    } 
}
 
module.exports = CoustomTransport;